package Day2025_03_12;

public class LeetCode740 {
    public int deleteAndEarn(int[] arr) {
        int n = 10001;
        int[] nums = new int[n];
        for (int x : arr) nums[x] += x;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = nums[0]; g[0] = 0;
        for (int i = 1; i < n; i++) {
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(f[i - 1], g[i - 1]);
        }
        return Math.max(f[n - 1], g[n - 1]);
    }
}
